package com.banksoft.admin.controller;

import com.banksoft.admin.dao.PartnerApplyDao;
import com.banksoft.admin.dao.UserDao;
import com.banksoft.admin.entity.PartnerApply;
import com.banksoft.admin.entity.User;
import com.banksoft.admin.entity.UserAddress;
import com.banksoft.admin.service.BaseService;
import com.banksoft.admin.service.PartnerApplyService;
import com.banksoft.admin.service.UserAddressService;
import com.banksoft.common.BaseController;
import com.banksoft.common.PageInfo;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vanjoger on 2017/1/5.
 */
@Controller
@RequestMapping("/partnerApply")
public class PartnerApplyController extends BaseController{

    @Autowired
    BaseService baseService;
    @Autowired
    PartnerApplyService partnerApplyService;
    @Autowired
    UserAddressService userAddressService;


    @RequestMapping("/list")
    public String list(String type,Model model){
        model.addAttribute("type",type);
        return "member/partner_apply_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String nickName,String startDate,String endDate,String status,PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        if(StringUtil.isNotNull(startDate))
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        if(StringUtil.isNotNull(endDate))
            map.put("endDate",DateUtil.getLongFormStrDate(endDate));
        map.put("status",status);
        if(StringUtil.isNotNull(nickName))
            map.put("nickName",nickName);

        pageInfo.setParams(map);

        pageInfo = this.page(PartnerApplyDao.class,pageInfo);

        return this.getPageResult(pageInfo);
    }

    @RequestMapping("/beforeEdit")
    public String beforeEdit(String id,Model model){
        if(StringUtil.isNotNull(id)){
            PartnerApply data = this.baseService.findById(PartnerApplyDao.class,id);
            if(StringUtil.isNotNull(data.getUserId())){
                List<UserAddress> list = this.userAddressService.listByUserId(data.getUserId());
                if(StringUtil.isNotEmpty(list)){
                    UserAddress address = null;
                    for(UserAddress userAddress:list){
                        if("0".equals(userAddress.getIsDefault())){
                            address = userAddress;
                            break;
                        }
                    }
                    if(address==null){
                        address = list.get(0);
                    }
                    model.addAttribute("address",address);
                }
                User user = this.baseService.findById(UserDao.class,data.getUserId());
                model.addAttribute("user",user);
            }
            model.addAttribute("data",data);
        }
        return "member/partner_apply_view";
    }

    @RequestMapping("/save")
    @ResponseBody
    public JSONObject save(String id,String status,String bz,HttpServletRequest request){
        try{
            JSONObject json = this.partnerApplyService.save(id,status,bz);
            if(json.getBoolean("success")){
                if("1".equals(status)){
                    this.saveLog("审核通过区域合伙人申请:"+id,request);
                }else if("2".equals(status)){
                    this.saveLog("审核拒绝区域合伙人申请:"+id,request);
                }
            }
            return json;
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,"保存失败",null);
    }




}
